package com.hk.mybatis.query;

import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

/**
 * 动态条件分组，支持多种组合：
 * select * from table where col1=val1 and col2 = val2
 * select * from table where col1=val1 or col2 = val2
 * select * from table where col1=val1 and (col2 = val2 or col3 = val3)
 * select * from table where col1=val1 and (col2 = val2 and col3 = val3)
 * select * from table where col1=val1 or (col2 = val2 or col3 = val3)
 * select * from table where col1=val1 or (col2 = val2 and col3 = val3)
 * select * from table where col1=val1 and (col2 = val2 or col3 = val3) or col4=val4
 */
@Getter
public class ConditionGroup {
    /**
     * 查询条件
     * -- GETTER --
     *  获取查询条件
     *
     * @return {@link List< QueryCondition >} 返回查询条件

     */
    private List<QueryCondition> conditions = new ArrayList<>();

    /**
     * 追加查询条件分组
     * -- GETTER --
     *  获取追加查询条件分组
     *
     * @return {@link List< ConditionGroup >} 返回追加查询条件分组

     */
    private List<ConditionGroup> subConditionGroups = new ArrayList<>();

    /**
     * 追加查询条件关系
     * -- GETTER --
     *  获取追加查询条件关系
     *
     * @return {@link String} 返回追加查询条件关系

     */
    private String queryRelation = QueryRelation.AND;

    public ConditionGroup() {
    }

    /**
     * 动态条件分组构造方法
     *
     * @param conditions         {@link List< QueryCondition >} 查询条件
     * @param subConditionGroups {@link List< ConditionGroup >} 追加查询条件分组
     * @param queryRelation      {@link String} 追加查询条件关系
     */
    public ConditionGroup(List<QueryCondition> conditions, List<ConditionGroup> subConditionGroups, String queryRelation) {
        this.conditions = conditions;
        this.subConditionGroups = subConditionGroups;
        this.queryRelation = queryRelation;
    }

    /**
     * 设置查询条件
     *
     * @param conditions {@link List< QueryCondition >} 查询条件
     */
    public void setConditions(List<QueryCondition> conditions) {
        this.conditions = conditions;
    }

    /**
     * 设置追加查询条件分组
     *
     * @param subConditionGroups {@link List< QueryCondition >} 追加查询条件分组
     */
    public void setSubConditionGroups(List<ConditionGroup> subConditionGroups) {
        this.subConditionGroups = subConditionGroups;
    }

    /**
     * 设置追加查询条件关系
     *
     * @param queryRelation {@link String} 追加查询条件关系
     */
    public void setQueryRelation(String queryRelation) {
        this.queryRelation = queryRelation;
    }

    /**
     * 添加查询条件
     *
     * @param conditons 查询条件
     */
    public void addCondition(QueryCondition... conditons) {
        for (QueryCondition queryCondition : conditons) {
            getConditions().add(queryCondition);
        }
    }

    /**
     * 添加追加查询条件分组
     *
     * @param conditonGroups 查询条件分组
     */
    public void addSubConditionGroup(ConditionGroup... conditonGroups) {
        for (ConditionGroup conditionGroup : conditonGroups) {
            getSubConditionGroups().add(conditionGroup);
        }
    }
}